﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

public partial class CardCenter_OnlinePurchaseCardStep1 : System.Web.UI.Page
{
    GetProdunctInfo getProduct = null;
    DataTable shoppingTable = null;
    WebUtility util = new WebUtility();
    protected void Page_Load(object sender, EventArgs e)
    {
        //绑定购卡时DropDownList
        if (!Page.IsPostBack)
        {
            //创建购物车DataTable
            this.createShoppingTable();
            getProduct = new GetProdunctInfo();
            Dictionary<string, string> dic = getProduct.getProductAndPrice();
            ListItem[] listItems = new ListItem[dic.Count];
            ListItem productItem = null;
            int k = 0;
            foreach (KeyValuePair<string, string> item in dic)
            {
                string itemText = item.Key;
                string itemValue = item.Value;
                productItem = new ListItem(itemText, itemValue);
                listItems[k] = productItem;
                k++;
            }
            this.DDL_ProductList.Items.AddRange(listItems);
            this.DDL_ProductList.Items.Insert(0,new ListItem("-- 请选择 --",""));
            this.DDL_ProductList.SelectedIndex = 0;
        }

        if (Session["Mobile"] != null)
        {
           // this.Txt_Mobile.Text = Session["Mobile"].ToString();
        }
       
    }

    /// <summary>
    /// 构建购物Table
    /// </summary>
    public void createShoppingTable() 
    {
        if (Session["ShoppingCat"] != null)
        {
            this.shoppingTable = (DataTable)Session["ShoppingCat"];
        }
        else
        {
            this.shoppingTable = new DataTable("ShoppingCat");
            // DataTable 手机号、点数、张数、单价
            DataColumn id = new DataColumn("pid", typeof(System.String));

            DataColumn mobile = new DataColumn("Mobile",typeof(System.String));
            mobile.Caption = "手机号";

            DataColumn points = new DataColumn("Points",typeof(System.String));
            points.Caption = "类别";

            DataColumn cardNums = new DataColumn("CardNums", typeof(System.String));
            cardNums.Caption = "张数";

            DataColumn perPrice = new DataColumn("PerPrice", typeof(System.String));
            perPrice.Caption = "单价";

            //添加列到Table
            shoppingTable.Columns.Add(id);
            shoppingTable.Columns.Add(mobile);
            shoppingTable.Columns.Add(points);
            shoppingTable.Columns.Add(cardNums);
            shoppingTable.Columns.Add(perPrice);

            Session["ShoppingCat"] = this.shoppingTable;
        }
    }

    //-----------------------------------------------------------------------------

    //添加一张卡
    protected void ImgBtn_Add_Click(object sender, ImageClickEventArgs e)
    {
        string cardNums = this.Txt_CardNums.Text;
        int cardNum = Convert.ToInt32(cardNums);
        cardNum += 1;
        this.Txt_CardNums.Text = cardNum.ToString();
    }

    //删除一张卡
    protected void ImgBtnMins_Click(object sender, ImageClickEventArgs e)
    {
        string cardNums = this.Txt_CardNums.Text;
        int cardNum = Convert.ToInt32(cardNums);
        if(cardNum > 1)
        {
            cardNum -= 1;
            this.Txt_CardNums.Text = cardNum.ToString();
        }
    }
    //级联选择事件
    protected void DDL_ProductList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Txt_Price.Text = this.DDL_ProductList.SelectedValue;
    }

    //挑选完成进入下一步
    protected void ImgBtn_Next_Click(object sender, ImageClickEventArgs e)
    {
        string mobile = this.Txt_Mobile.Text;
        string cardType = this.DDL_ProductList.SelectedItem.Text;
        string perPrice = this.DDL_ProductList.SelectedValue;
        string cardNums = this.Txt_CardNums.Text;

        DataTable table = (DataTable)Session["ShoppingCat"];

        DataRow row = table.NewRow();
        row["pid"] = Guid.NewGuid().ToString();
        row["Mobile"] = mobile;
        row["Points"] = cardType;
        row["CardNums"] = cardNums;
        row["PerPrice"] = perPrice;
        table.Rows.Add(row);
        //再放入购物车
      
        table.AcceptChanges();
        Session["ShoppingCat"] = table;
        Session["Mobile"] = mobile;
        Server.Transfer("OnlinePurchaseCardStep2.aspx");
        //int j = ((DataTable)Session["ShoppingCat"]).Rows.Count;
        //string Mobile = ((DataTable)Session["ShoppingCat"]).Rows[0]["Mobile"].ToString();

    }
}
